package de.tu_darmstadt.gdi1.battleship.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import de.tu_darmstadt.gdi1.battleship.model.BattleshipGame;
import de.tu_darmstadt.gdi1.battleship.model.Event;

public class HighscoreFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -997252940717220555L;

	private BattleshipGame bsg;

	private JPanel contentPane;
	private JLabel lblHIG;

	private JPanel panel;
	private JLabel lblRank;
	private JLabel lblName;
	private JLabel lblTurns;
	private JLabel lblDuration;
	private JLabel lblDate;
	private JButton btnOK;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HighscoreFrame frame = new HighscoreFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	private HighscoreFrame() {
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getLblHIG(), BorderLayout.NORTH);
		contentPane.add(getPanel(), BorderLayout.CENTER);
		contentPane.add(getBtnOK(), BorderLayout.SOUTH);
	}

	/**
	 * Creates the Highscore frame with entries.
	 */
	public HighscoreFrame(BattleshipGame bsg) {
		this();
		this.bsg = bsg;
	}
/**
 * sets the title "Highscore"
 * @return
 */
	public JLabel getLblHIG() {
		if (lblHIG == null) {
			lblHIG = new JLabel(Messages.getString("Highscore.lblHIG"));
			lblHIG.setFont(new Font("Dialog", Font.BOLD, 19));
			lblHIG.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblHIG;
	}
/**
 * sets the titles such as rank or game duration
 * @return
 */
	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new GridLayout(0, 5, 0, 0));
			panel.add(getLblRank());
			panel.add(getLblName());
			panel.add(getLblTurns());
			panel.add(getLblDuration());
			panel.add(getLblDate());
		}
		return panel;
	}
/**
 * Updates the Highscore pannel if a new entry has to be made 
 */
	public void update() {

		clearList();

		Vector<Event> events = bsg.getHighscorelist().getScoreList();
		int rank = 1;
		for (final Event e : events) {
			final int r = rank;
			final Font f = new Font("Dialog", Font.PLAIN, 11);
			// Rank
			panel.add(new JLabel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 5071287521683393543L;

				{
					setText(Integer.toString(r) + ".");
					setFont(f);
				}
			});
			rank++;
			// Name, editierbar falls zuletzt erspielt.
			if (bsg.isUserGotHighscore()
					&& e == bsg.getHighscorelist().getLastAdded()) {
				panel.add(new JTextPane() {
					/**
					 * 
					 */
					private static final long serialVersionUID = 2963352497095036851L;

					{
						setText(e.getName());
						setFont(f);
						addKeyListener(new KeyListener() {

							@Override
							public void keyTyped(KeyEvent e) {
							}

							@Override
							public void keyReleased(KeyEvent e) {
							}

							@Override
							public void keyPressed(KeyEvent e) {
								if (e.getKeyCode() == KeyEvent.VK_ENTER) {
									setEditable(false);
									bsg.setPlayerName(getText());
									setBackground(Color.GREEN);
									bsg.getHighscorelist().getLastAdded().setName(bsg.getPlayerName());
									getBtnOK().setText(Messages.getString("Highscore.btnOK"));
									getBtnOK().setEnabled(true);
								}
							}
						});
					}
				});
			} else {
				panel.add(new JLabel() {
					/**
					 * 
					 */
					private static final long serialVersionUID = -1306281839844829324L;

					{
						setText(e.getName());
						setFont(f);
					}
				});
			}
			// Turns
			panel.add(new JLabel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 2154711556937712855L;

				{
					setText(Integer.toString(e.getTurnNumber()));
					setFont(f);
				}
			});
			// Duration
			panel.add(new JLabel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = -2827267100108770443L;

				{
					setText(e.getDurationString());
					setFont(f);
				}
			});
			// Date
			panel.add(new JLabel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 4302472877119598337L;

				{
					setText(e.getDateString());
					setFont(f);
				}
			});

		}

		this.pack();
	}
/**
 * clears the whole Highscore panel, used before updating it
 */
	private void clearList() {
		contentPane.remove(panel);
		panel = null;
		lblRank = null;
		lblName = null;
		lblTurns = null;
		lblDuration = null;
		lblDate = null;
		contentPane.add(getPanel(), BorderLayout.CENTER);
	}
//following methods set titles
	public JLabel getLblRank() {
		if (lblRank == null) {
			lblRank = new JLabel(Messages.getString("Highscore.lblRank"));
		}
		return lblRank;
	}

	public JLabel getLblName() {
		if (lblName == null) {
			lblName = new JLabel(Messages.getString("Highscore.lblName"));
		}
		return lblName;
	}

	public JLabel getLblTurns() {
		if (lblTurns == null) {
			lblTurns = new JLabel(Messages.getString("Highscore.lblTurns"));
		}
		return lblTurns;
	}

	public JLabel getLblDuration() {
		if (lblDuration == null) {
			lblDuration = new JLabel(Messages.getString("Highscore.lblDuration"));
		}
		return lblDuration;
	}

	public JLabel getLblDate() {
		if (lblDate == null) {
			lblDate = new JLabel(Messages.getString("Highscore.lblDate"));
		}
		return lblDate;
	}
/**
 * creates the ok Button to close the list
 * @return
 */
	public JButton getBtnOK() {
		if (btnOK == null) {
			btnOK = new JButton(Messages.getString("Highscore.btnOK_1"));
			btnOK.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					hideHighscore();
				}
			});
			btnOK.setEnabled(false);
		}
		return btnOK;
	}
	
	public void hideHighscore() {
		this.setVisible(false);
	}
	
	public void updateLabels(){
		lblHIG.setText(Messages.getString("Highscore.lblHIG"));
		lblRank.setText(Messages.getString("Highscore.lblRank"));
		lblName.setText(Messages.getString("Highscore.lblName"));
		lblTurns.setText(Messages.getString("Highscore.lblTurns"));
		lblDuration.setText(Messages.getString("Highscore.lblDuration"));
		lblDate.setText(Messages.getString("Highscore.lblDate"));
		btnOK.setText(Messages.getString("Highscore.btnOK"));

	}
}
